package No1_100.No81_90.MergeSortedArray_88;

import java.util.Arrays;

public class Solution {
	/*
	 * 在原数组中直接变化，可以返回
	 * */
    public static void merge(int[] n1, int m, int[] n2, int n) {
        int i = m-1;
        int j = n-1;
        int k = m+n-1;
        while(i>=0 && j>=0){
            if(n1[i] > n2[j]){
                n1[k--] = n1[i--];
            }else{
                n1[k--] = n2[j--];
            }
        }
        while(i>=0){
            n1[k--] = n1[i--];
        }
        while(j>=0){
            n1[k--] = n2[j--];
        }
    }
	
    public static void merge2(int[] n1, int m, int[] n2, int n) {
        int[] c = new int[m+n];
        int i = 0,j = 0,k = 0;
        while(i<m && j<n){
            if(n1[i]<=n2[j]){
                c[k++] = n1[i++];
            }else{
                c[k++] = n2[j++];
            }
        }
        while(i < m){
            c[k++] = n1[i++];
        }
        while(j < n){
            c[k++] = n2[j++];
        }
        n1 = c;//这种方式只是把n1的引用改了，没用
    }
	public static int[] merge1(int[] nums1, int m, int[] nums2, int n) {
		int[] nums3 = new int[m+n];
		if (m == 0) {
			nums3 = nums2;
		}
		if (n == 0) {
			nums3 = nums1;
		}
		int j = 0,k = 0;
        for (int i = 0; i < m; i++) {
			for (; j < n; j++) {
				if (nums1[i] <= nums2[j]) {
					nums3[k++] = nums1[i];
					break;
				}
				else {
					nums3[k++] = nums2[j];
				}
			}
		}
        nums1 = nums3;
        for (int i = 0; i < nums1.length; i++) {
			System.out.println(nums1[i]);
		}
        return nums1;
    }
	
	public static void main(String[] args) {
		int[] nums1 = {0,1,2};
		int[] nums2 = {1};
		merge(nums1,0,nums2,1);
		System.out.println(Arrays.toString(nums1));
		
//		int[] nums3 = merge(nums1,0,nums2,1);
//		for (int i = 0; i < nums3.length; i++) {
//			System.out.println(nums3[i]);
//		}
	}
}
